package com.hanwei.uap.acceptors.api.utils;

import com.hanwei.uap.acceptors.api.constants.ChannelKey;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;

import java.net.InetSocketAddress;

public class ChannelUtils {
    public static String getRemoteHost(Channel channel) {
        if (channel == null)
            return null;
        InetSocketAddress remoteAddress = (InetSocketAddress) channel.remoteAddress();
        String remoteHost = remoteAddress.getAddress().getHostAddress();
        return remoteHost;
    }

    public static int getRemotePort(Channel channel) {
        if (channel == null)
            return 0;
        InetSocketAddress remoteAddress = (InetSocketAddress) channel.remoteAddress();
        int remotePort = remoteAddress.getPort();
        return remotePort;
    }

    public static String getDeviceId(Channel channel) {
        AttributeKey<String> deviceIdKey = AttributeKey.valueOf(ChannelKey.DEVICEID);
        Attribute<String> deviceIdAttr = channel.attr(deviceIdKey);
        String deviceId = deviceIdAttr.get();
        return deviceId;
    }

    public static String getDeviceId(ChannelHandlerContext ctx) {
        AttributeKey<String> deviceIdKey = AttributeKey.valueOf(ChannelKey.DEVICEID);
        Attribute<String> deviceIdAttr = ctx.channel().attr(deviceIdKey);
        String deviceId = deviceIdAttr.get();
        return deviceId;
    }

    public static String getAttribute(Channel channel, String channelKey){
        AttributeKey<String> attrKey = AttributeKey.valueOf(channelKey);
        Attribute<String> attr = channel.attr(attrKey);
        return attr.get();
    }

    public static void setDeviceType(Channel channel, String channelKey, Integer value){
        AttributeKey<Integer> attrKey = AttributeKey.valueOf(channelKey);
        Attribute<Integer> attr = channel.attr(attrKey);
        attr.set(value);
    }
    public static Integer getDeviceType(Channel channel, String channelKey){
        AttributeKey<Integer> attrKey = AttributeKey.valueOf(channelKey);
        Attribute<Integer> attr = channel.attr(attrKey);
        return attr.get();
    }

    public static void setDeviceFullId(Channel channel, String channelKey, String value){
        AttributeKey<String> attrKey = AttributeKey.valueOf(channelKey);
        Attribute<String> attr = channel.attr(attrKey);
        attr.set(value);
    }
    public static String getDeviceFullId(Channel channel, String channelKey){
        AttributeKey<String> attrKey = AttributeKey.valueOf(channelKey);
        Attribute<String> attr = channel.attr(attrKey);
        return attr.get();
    }

    public static void setAttribute(Channel channel, String channelKey, String value){
        AttributeKey<String> attrKey = AttributeKey.valueOf(channelKey);
        Attribute<String> attr = channel.attr(attrKey);
        attr.set(value);
    }

    public static String setAttributeIfAbsent(Channel channel, String channelKey, String value){
        AttributeKey<String> attrKey = AttributeKey.valueOf(channelKey);
        Attribute<String> attr = channel.attr(attrKey);
        return attr.setIfAbsent(value);
    }
}
